from models import Restaurant
from functools import wraps

from django.views.generic import simple
def add_resturant(view_func):
    """
    A wrapper which adds a 'restaurant' parameter to a view function's keyword 
    argumens that contains the restaurant associated with the current site.
    """
    @wraps(f)
    def _wrapper(*args, **kwargs):
        site_restaurants = Restaurant.on_site.all()        
        restaurant = len(site_restaurants) > 0 and site_restaurants[0] or None
        kwargs['restaurant'] = restaurant
        return f(*args, **kwargs)
    return _wrapper


@add_resturant
def index(request, restaurant=None):
    """Main site index view"""
    return simple.direct_to_template(
        request,
        template='restaurant/index.html',
        extra_context = {'restaurant': restaurant}
    )
        